'use strict';
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const db = uniCloud.database({});
	const shop = db.collection('db_shop_bag')
	let dd = []
	// shop.aggregate().match({
	// 	uid: event.item_id,
	// 	type: event.type
	// }).lookup({
	// 	from: 'db_shop',
	// 	localField: 'item_id',
	// 	foreignField: 'item_id',
	// 	as: 'shop'
	// }).end().then((res) => {
	// 	dd = res.data
	// 	for (let i = 0; i < dd.length; i++) {
	// 		dd[i]["img"] = dd[i]["shop"][0]["item_img"]
	// 		dd[i]["title"] = dd[i]["shop"][0]["item_title"]
	// 		dd[i]["content"] = dd[i]["shop"][0]["item_content"]
	// 		dd[i]["price"] = dd[i]["shop"][0]["item_price"]
	// 		dd[i]["id"] = dd[i]["shop"][0]["_id"]
	// 		dd[i]["item_shops"] = dd[i]["shop"][0]["item_shops"]
	// 		dd[i]["flat"] = false

	// 	}
	// })

	for (let i = 0; i < event._id.length; i++) {
		await shop.aggregate().match({
			_id: event._id[i],
		}).lookup({
			from: 'db_shop',
			localField: 'item_id',
			foreignField: 'item_id',
			as: 'shop'
		}).end().then(res => {
			console.log(res.data[0], i)
			dd.push(res.data[0])
			for (let i = 0; i < dd.length; i++) {
				dd[i]["img"] = dd[i]["shop"][0]["item_img"]
				dd[i]["title"] = dd[i]["shop"][0]["item_title"]
				dd[i]["content"] = dd[i]["shop"][0]["item_content"]
				dd[i]["price"] = dd[i]["shop"][0]["item_price"]
				dd[i]["id"] = dd[i]["shop"][0]["_id"]
				dd[i]["item_shops"] = dd[i]["shop"][0]["item_shops"]
				dd[i]["flat"] = false

			}
		})

	}
	// for (let i = 0; i < event._id.length; i++) {
	// 	await shop.where({
	// 		_id: event._id[i]
	// 	}).get().then(res => {
	// 		console.log(i, res.data[0])
	// 		dd.push(res.data[0])
	// 	})

	// }
	return dd
};